/**
 * Custom ArrayAdapter sebagai ListView terkustomisasi
 * Isinya nanti adalah LevelListViewItem
 */

package dp.mobile.level;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import dp.mobile.eden.MainMenuAct;
import dp.mobile.eden.R;
import dp.mobile.eden.SkirmishAct;
import dp.mobile.eden.SkirmishLevelSelectAct;

public class LevelArrayAdapter extends ArrayAdapter<LevelListViewItem>{
	public LevelArrayAdapter(Activity context, List<LevelListViewItem> list){
		super(context, R.layout.skirmish_level_item, list);
		
		mContext	= context;
		mList		= list;
	}
	
	static class ViewHolder {
		protected Button levelBtn;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder		= null;
		LayoutInflater inflator		= mContext.getLayoutInflater();
		final LevelListViewItem currItem = getItem(position);
	
		//Always Override convertView (not efficient but it keeps the ListView item consistent)  
		convertView	= inflator.inflate(R.layout.skirmish_level_item, null);
	
		//Bind convertView to viewHolder
		viewHolder	= new ViewHolder();
		convertView.setTag(viewHolder);
		
		//Get the View
		viewHolder.levelBtn	= (Button)	convertView.findViewById(R.id.level_button);
		
		//Set the View
		viewHolder.levelBtn.setText(currItem.getLevelTitle());
		viewHolder.levelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(((SkirmishLevelSelectAct)mContext).mIntentExtra == MainMenuAct.DOWNLOAD_LEVEL_INTENT){
					((SkirmishLevelSelectAct)mContext).downloadLevel(currItem.getLevelId() , currItem.getLevelTitle());
				}
				else{
					Intent intent = new Intent(mContext, SkirmishAct.class);
					intent.putExtra("level", currItem.getLevelFile());
					mContext.startActivity(intent);
					mContext.finish();
				}
			}
		});
		
		return convertView;
	}
	
	//ATTRIBUTES
	private final List<LevelListViewItem> mList;
	private final Activity mContext;
}
